package com.itheima.project.hystrix;

import com.itheima.project.basic.ResponseWrap;
import com.itheima.project.core.enums.search.SearchEnum;
import com.itheima.project.feign.SearchFeign;
import com.itheima.project.utils.ResponseWrapBuild;
import com.itheima.project.vo.search.RequestParamVo;
import com.itheima.project.vo.search.SearchPageVo;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 服务降级
 * @author mrchen
 * @date 2022/4/3 15:36
 */
@Component
@Slf4j
public class SearchHystrix  implements FallbackFactory<SearchFeign> {
    @Override
    public SearchFeign create(Throwable throwable) {
        // 远程调用异常对象
        throwable.printStackTrace();
        return new SearchFeign() {
            @Override
            public ResponseWrap<Boolean> saveDoc(String indexName, Object source) {
                return ResponseWrapBuild.build(SearchEnum.DOCUMENT_CREATE_FAIL,null);
            }
            @Override
            public ResponseWrap<Boolean> deleteDoc(String indexName, String id) {
                return ResponseWrapBuild.build(SearchEnum.DOCUMENT_DELETE_FAIL,null);
            }
            @Override
            public ResponseWrap<Map> getById(String indexName, String id) {
                return ResponseWrapBuild.build(SearchEnum.DOCUMENT_SEARCH_FAIL,null);
            }
            @Override
            public ResponseWrap<Boolean> saveDocBatch(String indexName, List<Object> list) {
                return ResponseWrapBuild.build(SearchEnum.DOCUMENT_CREATE_FAIL,null);
            }
            @Override
            public ResponseWrap<SearchPageVo<Map>> searchDoc(String indexName, RequestParamVo paramVo) {
                return ResponseWrapBuild.build(SearchEnum.DOCUMENT_SEARCH_FAIL,null);
            }

            @Override
            public ResponseWrap<SearchPageVo<Object>> searchDocPageResult(String indexName, String searchContent) {
                return ResponseWrapBuild.build(SearchEnum.DOCUMENT_SEARCH_FAIL,null);
            }

            @Override
            public ResponseWrap<String> searchDocNativeResult(String indexName, String searchContent) {
                return ResponseWrapBuild.build(SearchEnum.DOCUMENT_SEARCH_FAIL,null);
            }

            @Override
            public ResponseWrap<Boolean> isExistIndex(String indexName) {
                return ResponseWrapBuild.build(SearchEnum.INDEX_GET_FAIL,null);
            }
            @Override
            public ResponseWrap<Boolean> createIndex(String indexName, String source) {
                return ResponseWrapBuild.build(SearchEnum.INDEX_CREATE_FAIL,null);
            }
            @Override
            public ResponseWrap<Boolean> deleteIndex(String indexName) {
                return ResponseWrapBuild.build(SearchEnum.INDEX_DELETE_FAIL,null);
            }
        };
    }
}
